package desmontec.aluguel

import desmontec.alertas.AlertaMaquinaService;

class ConsumoAluguelController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	AlertaMaquinaService alertaMaquinaService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [consumoAluguelInstanceList: ConsumoAluguel.list(params), consumoAluguelInstanceTotal: ConsumoAluguel.count()]
    }

    def create = {
		System.out.println("create");
        def consumoAluguelInstance = new ConsumoAluguel()
        consumoAluguelInstance.properties = params
        return [consumoAluguelInstance: consumoAluguelInstance]
    }

    def save = {
		System.out.println("save");
        def consumoAluguelInstance = new ConsumoAluguel(params)
        if (consumoAluguelInstance.save(flush: true)) {
			System.out.println("vou chamar");
            //fernando.alves - inicio
			alertaMaquinaService.atualizaAlertasManutencao(consumoAluguelInstance.aluguel)
			//fernando.alves - fim
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), consumoAluguelInstance.id])}"
            redirect(action: "show", id: consumoAluguelInstance.id)
        }
        else {
            render(view: "create", model: [consumoAluguelInstance: consumoAluguelInstance])
        }
    }

    def show = {
        def consumoAluguelInstance = ConsumoAluguel.get(params.id)
        if (!consumoAluguelInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
            redirect(action: "list")
        }
        else {
            [consumoAluguelInstance: consumoAluguelInstance]
        }
    }

    def edit = {
        def consumoAluguelInstance = ConsumoAluguel.get(params.id)
        if (!consumoAluguelInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [consumoAluguelInstance: consumoAluguelInstance]
        }
    }

    def update = {
        def consumoAluguelInstance = ConsumoAluguel.get(params.id)
        if (consumoAluguelInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (consumoAluguelInstance.version > version) {
                    
                    consumoAluguelInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel')] as Object[], "Another user has updated this ConsumoAluguel while you were editing")
                    render(view: "edit", model: [consumoAluguelInstance: consumoAluguelInstance])
                    return
                }
            }
            consumoAluguelInstance.properties = params
            if (!consumoAluguelInstance.hasErrors() && consumoAluguelInstance.save(flush: true)) {
				//fernando.alves - inicio
				alertaMaquinaService.atualizaAlertasManutencao(consumoAluguelInstance.aluguel)
				//fernando.alves - fim
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), consumoAluguelInstance.id])}"
                redirect(action: "show", id: consumoAluguelInstance.id)
            }
            else {
                render(view: "edit", model: [consumoAluguelInstance: consumoAluguelInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
		System.out.println("Entreu no delete de ConsumoAluguelController");
        def consumoAluguelInstance = ConsumoAluguel.get(params.id)
        if (consumoAluguelInstance) {
            try {
                consumoAluguelInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
				//fernando.alves - inicio
				alertaMaquinaService.atualizaAlertasManutencao(consumoAluguelInstance.aluguel)
				//fernando.alves - fim
				redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'consumoAluguel.label', default: 'ConsumoAluguel'), params.id])}"
            redirect(action: "list")
        }
    }
	
	
	
}
